/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.youth.fund.hk.hostManager.common.Constants;
import com.youth.fund.hk.hostManager.manager.datamodel.AboutUsModel;
import com.youth.fund.hk.hostManager.manager.service.AboutUsService;

/**
 * @author Sunk_Sky
 *
 *         2014年11月14日下午7:16:43
 */
@Controller
@RequestMapping("/about_us")
public class AboutUsController
{
	@Autowired
	@Qualifier("AboutUsService")
	private AboutUsService service;

	@RequiresPermissions("about_us:view")
	@RequestMapping(method = RequestMethod.GET)
	public String list(HttpServletRequest req, Model model)
	{
		String lang;
		
		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}
	

		AboutUsModel aum = new AboutUsModel();
		// 这里要获取语言码，来进行操作
		aum.setLang(lang);

		model.addAttribute("about_usList", this.service.query(aum));

		return "about_us/list";
	}
	
	@RequiresPermissions("about_us:view")
	@RequestMapping(value = "/{language}",method = RequestMethod.GET)
	public String listLang(@PathVariable("language")String language,HttpServletRequest req, Model model)
	{
		String lang = language;

		req.getSession().setAttribute("local_lang", lang);
		
		AboutUsModel aum = new AboutUsModel();
		// 这里要获取语言码，来进行操作
		aum.setLang(lang);

		model.addAttribute("about_usList", this.service.query(aum));

		return "about_us/list";
	}

	@RequiresPermissions("about_us:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String showCreateForm(HttpServletRequest req, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}
		setCommonData(model,lang);
		
		AboutUsModel aum = new AboutUsModel();

		aum.setLang(lang);

		model.addAttribute("about_us", aum);

		model.addAttribute("op", "新增");

		return "about_us/edit";
	}

	@RequiresPermissions("about_us:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(HttpServletRequest req, AboutUsModel model,
			RedirectAttributes redirectAttributes)
	{
		boolean result = this.service.create(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "新增成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "新增失敗");
		}

		return "redirect:/about_us";
	}

	@RequiresPermissions("about_us:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(HttpServletRequest req, @PathVariable int id,
			Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}
		
		setCommonData(model,lang);

		AboutUsModel aum = new AboutUsModel();

		aum.setLang(lang);

		aum.setId(id);

		model.addAttribute("about_us", service.queryOne(aum));

		model.addAttribute("op", "修改");

		return "about_us/edit";
	}

	@RequiresPermissions("about_us:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(HttpServletRequest req, AboutUsModel model,
			RedirectAttributes redirectAttributes,@RequestParam("file")MultipartFile file)
	{
		System.out.println("==============="+file.getName());
		
		System.out.println(file.getSize());
		
		System.out.println("=============="+file.getContentType());
		
		boolean result = service.update(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "修改成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "修改失敗");
		}

		return "redirect:/about_us";
	}

	@RequiresPermissions("about_us:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String showDeleteForm(HttpServletRequest req, @PathVariable int id,
			Model model)
	{
		String lang;
		
		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}
		
		setCommonData(model,lang);

		AboutUsModel aum = new AboutUsModel();

		aum.setId(id);

		aum.setLang(lang);

		model.addAttribute("about_us", service.queryOne(aum));

		model.addAttribute("op", "刪除");

		return "about_us/edit";
	}

	@RequiresPermissions("about_us:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	public String delete(HttpServletRequest req, AboutUsModel model,
			RedirectAttributes redirectAttributes)
	{
		boolean result = service.delete(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "刪除成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "刪除失敗");
		}

		return "redirect:/about_us";
	}

	private void setCommonData(Model model,String lang)
	{
		AboutUsModel aum = new AboutUsModel();

		aum.setLang(lang);

		model.addAttribute("about_usList", this.service.query(aum));
	}
}
